package com.xuhai.movie.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xuhai.movie.bean.Movie;
import com.xuhai.movie.bean.MovieCategory;
import com.xuhai.movie.bean.MovieImage;
import com.xuhai.movie.mapper.MovieCategoryMapper;
import com.xuhai.movie.mapper.MovieImageMapper;
import com.xuhai.movie.mapper.MovieMapper;
import com.xuhai.movie.service.MovieService;
import com.xuhai.movie.util.PageUtil;
import com.xuhai.movie.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author PangJunjie
 * @Date 2022/4/13/013
 */
@Service
@Transactional
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieMapper movieMapper;

    @Autowired
    private MovieCategoryMapper movieCategoryMapper;

    @Autowired
    private MovieImageMapper movieImageMapper;

    @Override
    public ResultJson getPage(int page, int limit, Movie movie) {
        PageHelper.startPage(page,limit);
        List<Movie> movieList = movieMapper.getAll(movie);
        return ResultJson.ok().data(new PageUtil(new PageInfo<>(movieList)));
    }

    @Override
    public ResultJson getById(Integer movieId) {
        return ResultJson.ok().data(movieMapper.selectByPrimaryKey(movieId));
    }

    @Override
    public ResultJson getCategoryListByMovieId(Integer movieId) {
        return ResultJson.ok().data(movieCategoryMapper.getCategoryListByMovieId(movieId));
    }

    @Override
    public ResultJson getImageListByMovieId(Integer movieId) {
        return ResultJson.ok().data(movieImageMapper.getImageListByMovieId(movieId));
    }

    @Override
    public void saveOrUpdate(Movie movie) {
        if(movie.getMovieId() == null){
            /*添加电影信息*/
            movieMapper.insertSelective(movie);
        }else{
            /*修改前先删除该电影关联的分类和图片*/
            movieCategoryMapper.deleteByMovieId(movie.getMovieId());
            movieImageMapper.deleteByMovieId(movie.getMovieId());
            movieMapper.updateByPrimaryKeySelective(movie);
        }
        /*转换分类数组为集合*/
        if(!StringUtils.isEmpty(movie.getCategories())){
            List<MovieCategory> movieCategoryList = new ArrayList<>();
            String[] categoryList = movie.getCategories().split(",");
            for(String categoryId : categoryList){
                MovieCategory movieCategory = new MovieCategory();
                movieCategory.setMovieId(movie.getMovieId());
                movieCategory.setCategoryId(Integer.parseInt(categoryId));
                movieCategoryList.add(movieCategory);
            }
            /*批量添加分类信息*/
            movieCategoryMapper.batchInsert(movieCategoryList);
        }
        /*转换图片数组为集合*/
        if(!StringUtils.isEmpty(movie.getImages())){
            List<MovieImage> movieImageList = new ArrayList<>();
            String[] imageList = movie.getImages().split(",");
            for(String image:imageList){
                MovieImage movieImage = new MovieImage();
                movieImage.setMovieId(movie.getMovieId());
                movieImage.setImageSrc(image);
                movieImageList.add(movieImage);
            }
            movieImageMapper.batchInsert(movieImageList);
        }
    }



    @Override
    public void deleteById(Integer movieId) {
        movieCategoryMapper.deleteByMovieId(movieId);
        movieImageMapper.deleteByMovieId(movieId);
        movieMapper.deleteByPrimaryKey(movieId);
    }

    @Override
    public void status(Movie movie) {
        movieMapper.updateByPrimaryKeySelective(movie);
    }
}
